package weaver.interfaces.hitech.duss.utils;

import com.sap.mw.jco.IFunctionTemplate;
import com.sap.mw.jco.JCO;
import com.weaver.integration.datesource.SAPInterationOutUtil;
import com.weaver.integration.log.LogInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZCO_ORDER_BUDGET {
    /**
     *@Description: 调用创建预算SAP函数
     *@Param: [type, year, tableParam]
     *@return: java.lang.String
     *@Author: Dusongsong
     *@Date: 2022/9/7
     */
    public static Map<String, String> execute(String type, String year, List<Map<String, String>> tableParam) {
        Map<String, String> returnMap = new HashMap<>(  );
        SAPInterationOutUtil outUtil = new SAPInterationOutUtil();
        JCO.Client myConnection = (JCO.Client) outUtil.getConnection( type, new LogInfo() );
        myConnection.connect();
        // 实例名
        JCO.Repository myRepository = new JCO.Repository( "Repository", myConnection );
        //设置BAPI函数
        IFunctionTemplate ft = myRepository.getFunctionTemplate( "ZCO_ORDER_BUDGET" );
        JCO.Function bapi = ft.getFunction();
        //输入参数和结构处理
        JCO.ParameterList input = bapi.getImportParameterList();
        input.setValue( year, "P_GJAHR" );
        input.setValue( "1", "P_PERAB" );
        input.setValue( "12", "P_PERBI" );
        input.setValue( "USD", "P_WAERS" );
        //输入表的处理
        JCO.ParameterList inputtable = bapi.getTableParameterList();
        JCO.Table t1 = inputtable.getTable( "IT_FILE" );
        for (Map<String, String> map : tableParam) {
            t1.appendRow();
            t1.setValue( map.get( "AUFNR" ), "AUFNR" );
            t1.setValue( map.get( "KSTAR" ), "KSTAR" );
            t1.setValue( map.get( "WKG001" ), "WKG001" );
            t1.setValue( map.get( "WKG002" ), "WKG002" );
            t1.setValue( map.get( "WKG003" ), "WKG003" );
            t1.setValue( map.get( "WKG004" ), "WKG004" );
            t1.setValue( map.get( "WKG005" ), "WKG005" );
            t1.setValue( map.get( "WKG006" ), "WKG006" );
            t1.setValue( map.get( "WKG007" ), "WKG007" );
            t1.setValue( map.get( "WKG008" ), "WKG008" );
            t1.setValue( map.get( "WKG009" ), "WKG009" );
            t1.setValue( map.get( "WKG010" ), "WKG010" );
            t1.setValue( map.get( "WKG011" ), "WKG011" );
            t1.setValue( map.get( "WKG012" ), "WKG012" );
        }
        //执行函数
        myConnection.execute( bapi );
        //输出参数和结构处理
        //JCO.ParameterList outPrint = bapi.getExportParameterList();
        //输出表的处理
        JCO.ParameterList Table00 = bapi.getTableParameterList();
        //返回处理结果以及日志
        JCO.Table t = Table00.getTable( "IT_RETURN" );
        String ZFLAG = (String) t.getValue( "ZFLAG" );
        String OBJET = (String) t.getValue( "OBJET" );
        String MSG = (String) t.getValue( "MSG" );
        returnMap.put( "ZFLAG", ZFLAG );
        returnMap.put( "OBJET", OBJET );
        returnMap.put( "MSG", MSG );
        JCO.releaseClient( myConnection );
        return returnMap;
    }
}
